<?php

namespace app\admin\model\order;

use app\admin\model\bin\Native;
use app\admin\model\bin\Type;
use app\admin\model\Organization;
use think\Model;


class Rent extends Model
{

    

    

    // 表名
    protected $name = 'order_rent';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'bin_type_text',
        'bin_native_text',
        'renttime_text',
        'bin_origin_text',
        'currency_type_text',
        'deadlinetime_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }
    public function getBinTypeList()
    {
        $typeList=Type::order(['weigh'=>'desc','createtime','id'])->select();

        return $typeList;
    }

    public function getBinNativeList()
    {
        $nativeList=Native::order(['weigh'=>'desc','createtime','id'])->select();

        return $nativeList;
    }
    
    public function getBinOriginList()
    {
        return ['1' => __('Bin_origin 1'), '2' => __('Bin_origin 2')];
    }

    public function getCurrencyTypeList()
    {
        return ['1' => __('Currency_type 1'), '2' => __('Currency_type 2'), '3' => __('Currency_type 3'), '4' => __('Currency_type 4')];
    }

    public function yardList(){
        $yardList=Organization::getYard();
        return   $yardList;
    }

    public function getRenttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['renttime']) ? $data['renttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getBinTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['bin_type']) ? $data['bin_type'] : '');
        $list = $this->getBinTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBinNativeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['bin_native']) ? $data['bin_native'] : '');
        $list = $this->getBinNativeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getBinOriginTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['bin_origin']) ? $data['bin_origin'] : '');
        $list = $this->getBinOriginList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCurrencyTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['currency_type']) ? $data['currency_type'] : '');
        $list = $this->getCurrencyTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeadlinetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deadlinetime']) ? $data['deadlinetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setRenttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeadlinetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function type()
    {
        return $this->belongsTo('app\admin\model\bin\Type', 'bin_type', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function native()
    {
        return $this->belongsTo('app\admin\model\bin\Native', 'bin_native', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function customer()
    {
        return $this->belongsTo('app\admin\model\Organization', 'customer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function takeyard()
    {
        return $this->belongsTo('app\admin\model\Organization', 'take_yard_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function returnyard()
    {
        return $this->belongsTo('app\admin\model\Organization', 'return_yard_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'salesman_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
